<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$query = "select `M`.*,`U`.`name` as `recipient_name`,`U`.`sname` as `recipient_sname` from `messages` as `M`,`users` as `U` where `M`.`sender_id`='".$user->getUserId()."' and `M`.`show_sender` = '1' and `M`.`recipient_id` = `U`.`user_id` order by `time` desc";
$messages = $db->getTable($query);

$page = UV::readInt(strip_tags($route->GET("page")));
$limit = 10;
$count = count($messages);
$max_page = ceil($count / $limit);
if ($page < 1)
	$page = 1;
elseif ($page > $max_page)
	$page = $max_page;

$query .= " limit ".($page-1)*$limit.",$limit";
$messages = $db->getTable($query);

require_once ADMIN.DS."head.php";
?>

<div class="full">
	<div class="submenu_container">
		<a class="submenu_button" href='/admin/messages/received'>Полученные</a>
		<a class="submenu_button submenu_button_active" href='/admin/messages/sended'>Отправленные</a>
		<a class="submenu_button" href='/admin/messages/send'>Отправить</a>
	</div>
	<h3>
		Отправленные сообщения
		<span id='msgActs'>
			<span>C отмеченными: </span>
			<a style="cursor:pointer" id='delMsg'>Удалить</a>
		</span>
	</h3><?php
	if (count($messages) > 0)
	{
		echo "<table class='message_list'>";
		echo "<input type='hidden' id='userAs' value='sender' />";
		echo "<tr class='messages_header'><td><input type='checkbox' id='check_all_messages' /></td><td>Получатель</td><td>Тема письма</td><td>Текст письма</td><td>Время</td><td class='actions'>Действия</td></tr>";
		foreach($messages as $message)
		{
			$class = $message['read'] == 1 ? "" : "class='unreaded'";
			echo "<tr $class>";
			echo "<td><input type='checkbox' class='message_check' name='message[]' value='".$message['message_id']."' /></td>";
			echo "<td class='bolder'>".$message['recipient_name']."<br />".$message['recipient_sname']."</td>";
			echo "<td><a class='send' href='/admin/messages/read/".$message['message_id']."'>".$message['header']."</a></td>";
			echo "<td>".cropText(str_replace("\n","<br />",$message['text']))."</td>";
			echo "<td>".date("H:i d.m.Y",$message['time'])."</td>";
			echo "<td class='actions'><a class='send' href='/admin/messages/send/".$message['recipient_id']."'>Еще одно</a><br /><a class='delete' href='/admin/messages/delete/".$message['message_id']."'>Удалить</a></td>";
			echo "</tr>";
		}
		echo "</table>";
	}
	else
		echo "<h3 class='gag'>У вас нет ни одного отправленного сообщения</h3>";
	echo showPages($count,$limit);
?></div>

<?php require_once ADMIN.DS."footer.php"; ?>
